<?php
namespace Modules\Buy\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Buy\Entities\Arr;
use Modules\Buy\Http\Requests\ArrtRequest;
class ArrController extends Controller
{
    //显示列表
    public function index(Arr $arr)
    {
//        $data = Arr::paginate(10);
        $res = $arr->arrTree();
        return view('buy::arr.index', compact('res'));
    }

    //创建视图
    public function create(Arr $arr)
    {
        $arrs = Arr::where('pid',0)->get();
        return view('buy::arr.create',compact('arr','arrs'));
    }

    //保存数据
    public function store(ArrtRequest $request,Arr $arr)
    {
        $arr->fill($request->all());
        $arr->save();

        return redirect('/buy/arr')->with('success', '保存成功');
    }

    //显示记录
    public function show(Arr $field)
    {
        return view('buy::arr.show', compact('field'));
    }

    //编辑视图
    public function edit(Arr $arr)
    {
        $arrs = Arr::where('pid',0)->get();
        return view('buy::arr.edit', compact('arr','arrs'));
    }

    //更新数据
    public function update(ArrtRequest $request, Arr $arr)
    {
        $arr->update($request->all());
        return redirect('/buy/arr')->with('success','更新成功');
    }

    //删除模型
    public function destroy(Arr $arr)
    {
//        dd($arr->toArray());
//        $res = Arr::where('pid',$arr['id'])->get();
//        dd($res->toArray());
//        $res->delete();
        $arr->delete();
//        $arr->find($arr['id'])->posts()->delete();
//        Arr::find(1)->posts()->delete();
//        Arr::find(1)->posts()->delete();
        return redirect('buy/arr')->with('success','删除成功');
    }
}
